package com.atguigu.yygh.order.controller.api;

import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.order.service.WxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA
 *
 * @Date: 2023/1/6 - 15:58
 * @Author: Hyoung
 * @Description:
 */

@Api(tags = "微信支付接口")
@RestController
@RequestMapping("/api/order/wxpay")
public class ApiWXPayController {

    @Autowired
    private WxPayService wxPayService;

    @ApiOperation("获取支付二维码url")
    @GetMapping("/auth/createNative/{orderId}")
    public R createNative(@PathVariable Long orderId, HttpServletRequest request) {

        //校验用户登录状态
        AuthContextHolder.checkAuth(request);

        String remoteAddr = request.getRemoteAddr();
        String codeUrl = wxPayService.createNative(orderId, remoteAddr);
        return R.ok().data("codeUrl", codeUrl);
    }

    @ApiOperation("查询支付状态")
    @GetMapping("queryPayStatus/{orderId}")
    public R queryPayStatus(@PathVariable("orderId") Long orderId) {

        //调用查询接口
        boolean success = wxPayService.queryPayStatus(orderId);

        if (success) {
            return R.ok().message("支付成功");
        }
        return R.ok().message("支付中").code(25000);
    }

}